package com.woniu.humanresource.controller;

import com.woniu.humanresource.entity.JobCategory;
import com.woniu.humanresource.entity.JobRelation;
import com.woniu.humanresource.entity.PageBean;
import com.woniu.humanresource.entity.ResponseText;
import com.woniu.humanresource.exception.GeneralException;
import com.woniu.humanresource.service.JobCategoryService;
import com.woniu.humanresource.service.JobRelationService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class JobRelationController {

    @Resource
    JobRelationService jobRelationService;

    // 删除职位关系

    //提交删除申请
    @DeleteMapping("deleteApplyByPosition/{position}")
    public ResponseText deleteJobRelationByPosition(@PathVariable("position") String position) {
        ResponseText responseText = null;
        try {
            jobRelationService.deleteApplyByPosition(position);
            responseText = new ResponseText(null, 200, "提交删除职位关系申请成功");
        }catch (Exception e){
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }

    //获取职位关系具体信息
    @GetMapping("getJobRelationByPosition/{position}")
    public ResponseText selectJobRelationByPosition(@PathVariable("position") String position) {
        ResponseText<List<JobRelation>> responseText = null;
        try {
            List<JobRelation> jobRelationByPosition = jobRelationService.selectJobRelatinoByPosition(position);
            responseText = new ResponseText<>(jobRelationByPosition,200,"查询到职位关系的具体信息！");
        }catch (Exception e){
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }


    // 添加职位
    @PostMapping("insertJobRelation")
    public ResponseText insertJobRelation(@RequestBody JobRelation jobRelation) {
        ResponseText responseText = null;
        try {
            jobRelation.setJobrelstatus(0);
            jobRelationService.insertJobRelation(jobRelation);
            responseText = new ResponseText(null,200 ,"添加职位类别成功！");
        }catch (Exception e){
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }

    // 修改职位关系
    @PutMapping("updateJobRelation/{position}")
    public ResponseText updateJobRelation(@PathVariable("position") String position,
                                          @RequestBody JobRelation jobRelation) {
        ResponseText responseText = null;
        try {
            jobRelationService.updateJobRelation(jobRelation, position);
            responseText = new ResponseText(null, 200, "修改职位关系成功！");
        }catch (Exception e){
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }


    // 查询所有职位关系
    @GetMapping("getAllJobRelation/{pageIndex}/{pageSize}")
    public ResponseText selectAllJobRelation(@PathVariable("pageIndex") int pageIndex,
                                       @PathVariable("pageSize") int pageSize
                                       ){
        ResponseText<PageBean<JobRelation>> responseText = null;
        try {
            PageBean<JobRelation> allJobRelationpageBean = jobRelationService.getAllJobRelation(pageIndex,pageSize);
            responseText = new ResponseText<>(allJobRelationpageBean,200,"查询到所有职位关系！");
        }catch (Exception e){
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }
    //条件查询
    @PostMapping("getJobRelation/{pageIndex}/{pageSize}")
    public ResponseText getJobRelation(@PathVariable("pageIndex") int pageIndex,
                                       @PathVariable("pageSize") int pageSize,
                                       @RequestBody JobRelation jobRelation) {
        ResponseText responseText = null;
        try {
            PageBean<JobRelation> jobRelationpageBean = jobRelationService.getJobRelation(jobRelation,pageIndex,pageSize);
            responseText = new ResponseText<>(jobRelationpageBean,200,"条件查询职位关系！");
        }catch (Exception e){
            e.printStackTrace();
            throw new GeneralException();
        }
        return responseText;
    }
}
